import moment from 'moment/moment'
import request from '@/utils/request'

export function getCompany() {//公司列表
  return request({
    url: '/company/getCompany',
    method: 'post',
    
  })
}

export function debounce(func, wait, immediate) {
  let timeout, args, context, timestamp, result

  const later = function() {
    // 据上一次触发时间间隔
    const last = +new Date() - timestamp

    // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
    if (last < wait && last > 0) {
      timeout = setTimeout(later, wait - last)
    } else {
      timeout = null
      // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
      if (!immediate) {
        result = func.apply(context, args)
        if (!timeout) context = args = null
      }
    }
  }

  return function(...args) {
    context = this
    timestamp = +new Date()
    const callNow = immediate && !timeout
    // 如果延时不存在，重新设定延时
    if (!timeout) timeout = setTimeout(later, wait)
    if (callNow) {
      result = func.apply(context, args)
      context = args = null
    }

    return result
  }
}


export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if (('' + time).length === 10) time = parseInt(time) * 1000
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

export function formatTime(time, option) {
  time = +time * 1000
  const d = new Date(time)
  const now = Date.now()

  const diff = (now - d) / 1000

  if (diff < 30) {
    return '刚刚'
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + '分钟前'
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + '小时前'
  } else if (diff < 3600 * 24 * 2) {
    return '1天前'
  }
  if (option) {
    return parseTime(time, option)
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    )
  }
}

export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

export function baseUrl(){//图片或文件路径公共头
	return 'http://emp_new.quality-in.com';
}

export function baseApi(){
	return 'http://emp_new.quality-in.com';
}


export function safariDate(dates){
	return dates.split('-').join('/');
}

export function getMomentValue(val,times){//格式化时间
	if(!times){
		 return moment(val).format("YYYY-MM-DD");	
	}else{
		return moment(val).format("YYYY-MM-DD HH:mm:ss");	
	}
	
}

/*
 * 获取最近几天时间 
 * @param count  最近 count 天
 */
export function timeForMat (count) {
  // 拼接时间
  　　let time1 = new Date()
  　　time1.setTime(time1.getTime() - (24 * 60 * 60 * 1000))
  　　let Y1 = time1.getFullYear()
  　　let M1 = ((time1.getMonth() + 1) >= 10 ? (time1.getMonth() + 1) : '0' + (time1.getMonth() + 1))
  　　let D1 = (time1.getDate() > 10 ? time1.getDate()+1 : '0' + (time1.getDate()+1))
  　　let timer1 = Y1 + '-' + M1 + '-' + D1 // 当前时间
  　　let time2 = new Date()
  　　time2.setTime(time2.getTime() - (24 * 60 * 60 * 1000 * count))
  　　let Y2 = time2.getFullYear()
  　　let M2 = ((time2.getMonth() + 1) > 9 ? (time2.getMonth() + 1) : '0' + (time2.getMonth() + 1))
  　　let D2 = (time2.getDate() > 9 ? time2.getDate() : '0' + time2.getDate())
     let timer2 = Y2 + '-' + M2 + '-' + D2 // 之前的7天或者30天
  　　return {
       t1: timer1,
       t2: timer2
     } 
  }